import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

//绘制界面
public class GameWin extends JFrame {
    static int state;//表游戏状态,0未开始，1开始运行 2商店 3失败 4胜利
    public static long gameTime = 30;//倒计时初始时间
    public Thread start;//是否开始
    //存储金块、石块
    List<Object>objectList = new ArrayList<>();

    void launch() throws InterruptedException {
        this.setVisible(true);//窗口可见
        this.setSize(768, 1000);//窗口大小
        this.setLocationRelativeTo(null);//窗口居中
        this.setTitle("The Gold Miner");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);

                switch (state) {//不同状态发出不同指令
                    case 0://未开始
                        if (e.getButton() == 3 && e.getClickCount() == 2) {//左键出钩子，右键开始游戏和确认吃药水等功能
                            state = 1;
                            if(state == 1)
                            {
                                sky.startTime = System.currentTimeMillis();//进入时候给时间赋值
                            }
                        }
                        break;
                    case 1://左右摇摆，点击左键，空格下抓
                        if (e.getButton() == 1 && line.state == 0) {
                            line.state = 1;
                        }
                        //抓取返回，点击右键，药水减一
                        if (e.getButton() == 3 && line.state == 3&&Sky.yaoBasic>0) {//
                                Sky.yaoBasic--;
                                Sky.yaoState = true;
                        }
                        ;
                    case 2://进入商店,鼠标左键购买
                        if (e.getButton() == 1) {
                            sky.shop = true;//调整状态，购买药水
                        }
                        if (e.getButton() == 3 && e.getClickCount() ==2 ) {//双击右键进入下一关，并重新计时
                            state = 1;
                            sky.startTime = System.currentTimeMillis();
                        }
                        break;
                    case 3:
                    case 4:
                        //点左键重新开始,当结束或胜利时
                        if (e.getButton() == 1) {
                            state = 0;
                            Sky.total = 0;
                            sky.reGame();
                        }
                        break;
                    default:
                }


                if (e.getButton() == 1) {
                    line.state = 1;
                }
            }
        });

        while (true) {
            repaint();
            nextLevel();
            try{
                Thread.sleep(10);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
    public void nextLevel() throws InterruptedException {
        if(sky.gameTime()&&state==1){
            if(Sky.total>sky.goal) {
                if(Sky.level==5){state=4;}
                else{
                    state=2;
                    Sky.level++;
                    Sky.total += 10;
                    Sky.yaoBasic--;
                sky.startTime=System.currentTimeMillis();
                }//下一关开始时重置时间
               dispose();
               GameWin gameWin1 = new GameWin();
               gameWin1.launch();}
        else{
            state=3;}
        }
    }
    Sky sky = new Sky();
    line line = new line(this);

    //金块、石块
    {
        boolean isPlace = true;//是否可以放置
        for (int i = 0;i<10;i++)
        {
            double random = Math.random();
            Gold gold;
            if (random<0.3)
            {
                gold = new GoldPlus();
            }
            if(random<0.7)
            {
                gold = new Gold();
            }
            else
            {
                gold = new GoldMini();
            }
            for (Object obj:objectList)
            {
                //被占用
                if(gold.getRec().intersects(obj.getRec()))
                {
                    isPlace = false;
                }
            }
            if(isPlace)
            {
                objectList.add(gold);
            }
            else
            {
                isPlace = true;
                i--;
            }
        }

        for (int i = 0;i<7;i++)
        {
            Stone stone = new Stone();
            for (Object obj:objectList)
            {
                //被占用
                if(stone.getRec().intersects(obj.getRec()))
                {
                    isPlace = false;
                }
            }
            if(isPlace)
            {
                objectList.add(stone);
            }
            else
            {
                isPlace = true;
                i--;
            }
        }
    }

    Image worker1 = Toolkit.getDefaultToolkit().getImage("img/worker1.png");
    Image offScreenImage;

    @Override
    public void paint(Graphics g) {

        offScreenImage = this.createImage(768,1000);
        Graphics gImage = offScreenImage.getGraphics();

        Sky.paint(gImage);//背景绘制
        if(state==1) {
            for (Object obj : objectList) {//绘制物体
                obj.paintSelf(gImage);
            }
            gImage.drawImage(worker1, 310, 80, null);
            line.print_line(gImage);
        }
        g.drawImage(offScreenImage,0,0,null);
    }

    public static void main(String[] args) {
        GameWin gameWin = new GameWin();
        try {
            gameWin.launch();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
